home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / print.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  6KB  |  196 lines

  1. /* Output is only to the printer.  We need to test for printer
  2.  * connect before doing anything.  If no connect, return FALSE.
  3.  * These functions should allow line height and header selection
  4.  * via a menu.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <osbind.h>
  9. #include "ed.h"
  10. #include "printer.h"
  11.  
  12. /* PRINT eXtended command  Handles the print buffer function.  Gets user
  13.  * parameters, calls printer init, and reports errors.  Bound to ^X <TAB>.
  14.  */
  15.  
  16. static  char prnfin[NFILEN];
  17.  
  18. print(f, n)
  19. register int f;
  20. register int n;
  21. {
  22.  
  23.         register int s,i;
  24.         register int lm, rm, cw;
  25.         char buf[MYBUF];
  26.         lm = 6;         /* for now use 6 char left margins */
  27.         cw = 12;        /* for now use 12 char pitch */
  28.         i  = 0;
  29.  
  30.         if (n > 1)
  31.                 pagelen = n;    /* pagelen is set by routine in page.c */
  32.  
  33.         /* Eventually change these mode line questions into a window */
  34.         /* on the screen using the cursor keys to select options */
  35.  
  36.         if (isnprint || (PRNRDY == NULL))
  37.                 {
  38.                 mlwrite("Device LST: is being used");
  39.                 return(FALSE);
  40.                 }
  41.         if((s = readpattern("What is your name? ", &prnhdr[0])) != TRUE)
  42.                 return(s);
  43.         if((s = readpattern("What is today's date? ", &prndate[0])) != TRUE)
  44.                 return(s);
  45.         if((s = mlyesno("Do you want double spacing? ")) == FALSE)
  46.                 spacing = 1;
  47.         else
  48.                 spacing = 2;
  49.  
  50.         strcpy(prnfin,"~temp\\prn.fin");
  51.         parsefn(prnfin);
  52.         mlwrite("[Writing buffer file: %s page length = %d]", prnfin,pagelen);
  53.  
  54.         if(! tabsize)
  55.                 temptab = 8;
  56.         else
  57.                 temptab = tabsize;
  58.  
  59.         rm = 80;
  60.  
  61.         printout(prnfin, rm);   /* Temp file for buffer */
  62.         if((in = open(prnfin, 0)) == EOF)
  63.                 {
  64.                 mlwrite("OPEN failure on buffer file");
  65.                 return(FALSE);
  66.                 }
  67.         setprn(cw, spacing, temptab, lm, rm); /* Initialize printer */
  68.         isnprint = TRUE;                        /* We are printing */
  69.         /* Initially fill printer buffer */
  70.         if ((s = read(in, buf, MYBUF)) > NULL)
  71.                 while (s--)
  72.                         {
  73.                         if (buf[i] == '\n')
  74.                                 Cprnout('\r');
  75.                         if (!(int)gemdos(0x5,buf[i++]))
  76.                                 {
  77.                                 mlwrite("Write error on LST:");
  78.                                 return(FIOERR);
  79.                                 }
  80.                         }
  81.                 return(TRUE);
  82. }
  83.  
  84. /* SETPRN Local function to set printer parameters.  Mostly uses macros from
  85.  * printer.h
  86.  */
  87.  
  88. static int
  89. setprn(pitch, lheight, tabstop, lmarg, rmarg)
  90. register int pitch, lheight, tabstop, lmarg, rmarg;
  91. {
  92.         register int i;
  93.         pitch -= 2;
  94.         lheight = (lheight * 5) + 4;    /* this sort of gives single and */
  95.                                         /* double spacing @ 6 per inch */
  96.         RESET;
  97.         for(i=0;i<lmarg;i++)
  98.                 PRINT(SPACE);
  99.         SET_LMARG;
  100.         while(rmarg)
  101.                 {
  102.                 for(i=0;i<tabstop;i++,--rmarg)
  103.                         PRINT(SPACE);
  104.                 SET_TAB;
  105.                 }
  106.         PRINT(CR);
  107.         SET_PITCH(pitch);
  108.         SET_HEIGHT(lheight);
  109.         return(TRUE);
  110. }
  111.  
  112. /*
  113.  * This function performs the details of file buffer
  114.  * printing. Uses the file management routines in the
  115.  * "fileio.c" package.
  116.  */
  117.  
  118. static int
  119. printout(fn, marg)
  120. register char   *fn;
  121. register int marg;
  122. {
  123.         register int    s;
  124.         register LINE   *lp;
  125.         register int    nline;
  126.         register short f;
  127.  
  128.         f = FALSE;                              /* first time through */
  129.  
  130.         if ((s=ffwopen(fn)) != FIOSUC)          /* Open writes message. */
  131.                 return (FALSE);
  132.         lp = lforw(curbp->b_linep);             /* First line.          */
  133.         nline = 0;                              /* Number of lines.     */
  134.         linecnt = 0;
  135.         pagecnt = 1;
  136.         while (lp != curbp->b_linep)
  137.                 {
  138.                 if (linecnt == 0)
  139.                         {
  140.                         if((s=prhdg(f, pagecnt++, marg)) != TRUE)
  141.                                 break;
  142.                         f = TRUE;               /* Each new page gets \f */
  143.                         linecnt += 9;
  144.                         }
  145.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  146.                         break;
  147.                 ++nline;
  148.                 lp = lforw(lp);
  149.                 if(linecnt > pagelen - 3)
  150.                         {
  151.                         linecnt = 0;
  152.                         continue;
  153.                         }
  154.                 ++linecnt;
  155.         }
  156.         if (s == FIOSUC)                        /* No write error.      */
  157.                 s = ffclose();
  158.         if (s != FIOSUC)                        /* Some sort of error.  */
  159.                 return (FALSE);
  160.         return (TRUE);
  161. }
  162.  
  163. /* This function reads the temp file written above and writes the file to
  164.  * the list device.  It then deletes the temp file.
  165.  */
  166.  
  167. prnbuf()
  168. {
  169.         register int stat;
  170.         register int i;
  171.         char buf[BUFSIZE];
  172.         i = 0;
  173.  
  174.         if ((stat = read(in, buf, BUFSIZE)) > NULL)
  175.                 {
  176.                 while(stat--)
  177.                         {
  178.                         if (buf[i] == '\n')
  179.                                 Cprnout('\r');
  180.                         if (!(int)gemdos(0x5,buf[i++]))
  181.                                 {
  182.                                 mlwrite("Write error on LST:");
  183.                                 return(FIOERR);
  184.                                 }
  185.                         }
  186.                 return(TRUE);
  187.                 }
  188.         isnprint = FALSE;
  189.         close(in);
  190.         unlink(prnfin);
  191.         NEWPAGE;
  192.         mlwrite("[Printing completed]");
  193.         update();
  194.         return(TRUE);
  195. }
  196.